"""
Reference: 03 Efficient Post-processing with Importance Sampling in ShaderX7
"""
class Halton:
	def __init__(self, base = 13):
		self.value = 0.0
		self.inv_base = 1.0 / base
		
	def __next(self):
		r = 1.0 - self.value - 0.0000000001
		if self.inv_base < r:
			self.value = self.value + self.inv_base
		else:
			h = self.inv_base
			hh = 0
			
			while(True):
				hh = h
				h = h * self.inv_base
				
				if h <  r:
					break
			self.value = self.value + hh + h - 1.0
			
	def get_next(self):
		self.__next()
		return self.value
		
	def reset(self):
		self.value = 0
		
def ssao_sampler(num_of_samples):

	import math
	
	halton1 = Halton(2)
	halton2 = Halton(3)
	halton3 = Halton(5)
	
	samples = []
	i = 0
	
	while i < num_of_samples:
		x = halton1.get_next() * 2.0 - 1.0
		y = halton2.get_next() * 2.0 - 1.0
		
		if (x*x + y*y) > 1:
			continue
			
		z = math.sqrt(1.0 - x*x - y*y)
		s = halton3.get_next()
		r = s * s
		
		samples.append( (x*r, y*r, z*r) )
		
		i = i + 1
		
	return samples
		
if __name__ == "__main__":
	
	# read output filename
	filename = raw_input("output file? (default='ssao.txt')")
	if len(filename) == 0:
		filename = "ssao.txt"
	
	# read sample count
	cnt = raw_input("no. of samples? (default=16)")
	if len(cnt) == 0:
		cnt = "16"
	
	cnt = int(cnt)
	
	# generate samples
	samples = ssao_sampler(cnt)
	
	# output to file
	f = open(filename, "w")
	
	for s in samples:
		f.write("((float)%10f, (float)%10f, (float)%10f, 0);\n" % s)
		
	f.close()
	
	print("done")